{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import Scalar, draw_graph\n",
    "from linear_model import Linear, mse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"353pt\" height=\"517pt\"\n",
       " viewBox=\"0.00 0.00 353.21 516.80\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 512.797)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-512.797 349.205,-512.797 349.205,4 -4,4\"/>\n",
       "<!-- 140367041063952backward -->\n",
       "<g id=\"node1\" class=\"node\"><title>140367041063952backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M12,-224.938C12,-224.938 64.2051,-224.938 64.2051,-224.938 70.2051,-224.938 76.2051,-230.938 76.2051,-236.938 76.2051,-236.938 76.2051,-271.859 76.2051,-271.859 76.2051,-277.859 70.2051,-283.859 64.2051,-283.859 64.2051,-283.859 12,-283.859 12,-283.859 6,-283.859 0,-277.859 0,-271.859 0,-271.859 0,-236.938 0,-236.938 0,-230.938 6,-224.938 12,-224.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-271.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"0,-264.219 76.2051,-264.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"0,-244.578 76.2051,-244.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-232.578\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140367041063712backward -->\n",
       "<g id=\"node9\" class=\"node\"><title>140367041063712backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M68,-112.719C68,-112.719 120.205,-112.719 120.205,-112.719 126.205,-112.719 132.205,-118.719 132.205,-124.719 132.205,-124.719 132.205,-159.641 132.205,-159.641 132.205,-165.641 126.205,-171.641 120.205,-171.641 120.205,-171.641 68,-171.641 68,-171.641 62,-171.641 56,-165.641 56,-159.641 56,-159.641 56,-124.719 56,-124.719 56,-118.719 62,-112.719 68,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"94.1025\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"56,-152 132.205,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"94.1025\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"56,-132.359 132.205,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"94.1025\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140367041063952backward&#45;&gt;140367041063712backward -->\n",
       "<g id=\"edge12\" class=\"edge\"><title>140367041063952backward&#45;&gt;140367041063712backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M52.6697,-224.727C59.4752,-211.333 67.6683,-195.207 74.9657,-180.845\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"78.1989,-182.208 79.6083,-171.707 71.9582,-179.037 78.1989,-182.208\"/>\n",
       "<text text-anchor=\"middle\" x=\"91.1743\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140367041063904backward -->\n",
       "<g id=\"node13\" class=\"node\"><title>140367041063904backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"159,-112.719 159,-171.641 235.205,-171.641 235.205,-112.719 159,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"197.103\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;5.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"159,-152 235.205,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"197.103\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"159,-132.359 235.205,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"197.103\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">b</text>\n",
       "</g>\n",
       "<!-- 140367041063952backward&#45;&gt;140367041063904backward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>140367041063952backward&#45;&gt;140367041063904backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M76.2711,-229.764C79.2538,-227.952 82.2239,-226.16 85.1025,-224.438 98.7744,-216.258 102.904,-215.361 116.103,-206.438 129.076,-197.665 142.676,-187.472 154.946,-177.863\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"157.125,-180.603 162.798,-171.654 152.783,-175.112 157.125,-180.603\"/>\n",
       "<text text-anchor=\"middle\" x=\"160.174\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140367041063472backward -->\n",
       "<g id=\"node2\" class=\"node\"><title>140367041063472backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"129.103\" cy=\"-254.398\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"129.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">y1=1.00</text>\n",
       "</g>\n",
       "<!-- 140367041064000backward -->\n",
       "<g id=\"node3\" class=\"node\"><title>140367041064000backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M99,-337.156C99,-337.156 151.205,-337.156 151.205,-337.156 157.205,-337.156 163.205,-343.156 163.205,-349.156 163.205,-349.156 163.205,-384.078 163.205,-384.078 163.205,-390.078 157.205,-396.078 151.205,-396.078 151.205,-396.078 99,-396.078 99,-396.078 93,-396.078 87,-390.078 87,-384.078 87,-384.078 87,-349.156 87,-349.156 87,-343.156 93,-337.156 99,-337.156\"/>\n",
       "<text text-anchor=\"middle\" x=\"125.103\" y=\"-384.078\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"87,-376.438 163.205,-376.438 \"/>\n",
       "<text text-anchor=\"middle\" x=\"125.103\" y=\"-364.438\" font-family=\"Menlo\" font-size=\"10.00\">value=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"87,-356.797 163.205,-356.797 \"/>\n",
       "<text text-anchor=\"middle\" x=\"125.103\" y=\"-344.797\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140367041064000backward&#45;&gt;140367041063952backward -->\n",
       "<g id=\"edge8\" class=\"edge\"><title>140367041064000backward&#45;&gt;140367041063952backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M96.8068,-336.954C91.3717,-331.053 85.8534,-324.771 80.959,-318.656 74.4313,-310.5 67.8642,-301.376 61.8997,-292.652\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"64.6426,-290.458 56.1598,-284.11 58.8327,-294.363 64.6426,-290.458\"/>\n",
       "<text text-anchor=\"middle\" x=\"102.174\" y=\"-307.456\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140367041064000backward&#45;&gt;140367041063472backward -->\n",
       "<g id=\"edge9\" class=\"edge\"><title>140367041064000backward&#45;&gt;140367041063472backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M126.143,-336.946C126.884,-316.535 127.855,-289.784 128.479,-272.59\"/>\n",
       "</g>\n",
       "<!-- 140367041063520backward -->\n",
       "<g id=\"node4\" class=\"node\"><title>140367041063520backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"216.103\" cy=\"-254.398\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"216.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">y2=4.00</text>\n",
       "</g>\n",
       "<!-- 140367041063568backward -->\n",
       "<g id=\"node5\" class=\"node\"><title>140367041063568backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"94.1025\" cy=\"-29.9609\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"94.1025\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">x1=1.50</text>\n",
       "</g>\n",
       "<!-- 140367041064096backward -->\n",
       "<g id=\"node6\" class=\"node\"><title>140367041064096backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M193,-337.156C193,-337.156 245.205,-337.156 245.205,-337.156 251.205,-337.156 257.205,-343.156 257.205,-349.156 257.205,-349.156 257.205,-384.078 257.205,-384.078 257.205,-390.078 251.205,-396.078 245.205,-396.078 245.205,-396.078 193,-396.078 193,-396.078 187,-396.078 181,-390.078 181,-384.078 181,-384.078 181,-349.156 181,-349.156 181,-343.156 187,-337.156 193,-337.156\"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-384.078\" font-family=\"Menlo\" font-size=\"10.00\">grad=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"181,-376.438 257.205,-376.438 \"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-364.438\" font-family=\"Menlo\" font-size=\"10.00\">value=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"181,-356.797 257.205,-356.797 \"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-344.797\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140367041064096backward&#45;&gt;140367041063520backward -->\n",
       "<g id=\"edge14\" class=\"edge\"><title>140367041064096backward&#45;&gt;140367041063520backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M218.322,-336.946C217.767,-316.535 217.038,-289.784 216.57,-272.59\"/>\n",
       "</g>\n",
       "<!-- 140367041064288backward -->\n",
       "<g id=\"node11\" class=\"node\"><title>140367041064288backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M281,-224.938C281,-224.938 333.205,-224.938 333.205,-224.938 339.205,-224.938 345.205,-230.938 345.205,-236.938 345.205,-236.938 345.205,-271.859 345.205,-271.859 345.205,-277.859 339.205,-283.859 333.205,-283.859 333.205,-283.859 281,-283.859 281,-283.859 275,-283.859 269,-277.859 269,-271.859 269,-271.859 269,-236.938 269,-236.938 269,-230.938 275,-224.938 281,-224.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"307.103\" y=\"-271.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"269,-264.219 345.205,-264.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"307.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"269,-244.578 345.205,-244.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"307.103\" y=\"-232.578\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140367041064096backward&#45;&gt;140367041064288backward -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>140367041064096backward&#45;&gt;140367041064288backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M241.994,-336.946C252.994,-323.169 266.3,-306.502 278.01,-291.837\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"280.822,-293.924 284.326,-283.926 275.351,-289.557 280.822,-293.924\"/>\n",
       "<text text-anchor=\"middle\" x=\"289.174\" y=\"-307.456\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.00</text>\n",
       "</g>\n",
       "<!-- 140367041063616backward -->\n",
       "<g id=\"node7\" class=\"node\"><title>140367041063616backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"296.103\" cy=\"-29.9609\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">x2=2.00</text>\n",
       "</g>\n",
       "<!-- 140367041064144backward -->\n",
       "<g id=\"node8\" class=\"node\"><title>140367041064144backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M150,-449.375C150,-449.375 202.205,-449.375 202.205,-449.375 208.205,-449.375 214.205,-455.375 214.205,-461.375 214.205,-461.375 214.205,-496.297 214.205,-496.297 214.205,-502.297 208.205,-508.297 202.205,-508.297 202.205,-508.297 150,-508.297 150,-508.297 144,-508.297 138,-502.297 138,-496.297 138,-496.297 138,-461.375 138,-461.375 138,-455.375 144,-449.375 150,-449.375\"/>\n",
       "<text text-anchor=\"middle\" x=\"176.103\" y=\"-496.297\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"138,-488.656 214.205,-488.656 \"/>\n",
       "<text text-anchor=\"middle\" x=\"176.103\" y=\"-476.656\" font-family=\"Menlo\" font-size=\"10.00\">value=8.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"138,-469.016 214.205,-469.016 \"/>\n",
       "<text text-anchor=\"middle\" x=\"176.103\" y=\"-457.016\" font-family=\"Menlo\" font-size=\"10.00\">mse</text>\n",
       "</g>\n",
       "<!-- 140367041064144backward&#45;&gt;140367041064000backward -->\n",
       "<g id=\"edge11\" class=\"edge\"><title>140367041064144backward&#45;&gt;140367041064000backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M162.836,-449.165C156.638,-435.77 149.177,-419.645 142.531,-405.282\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"145.679,-403.75 138.303,-396.145 139.326,-406.69 145.679,-403.75\"/>\n",
       "<text text-anchor=\"middle\" x=\"170.96\" y=\"-419.675\" font-family=\"Menlo\" font-size=\"14.00\">1.00</text>\n",
       "</g>\n",
       "<!-- 140367041064144backward&#45;&gt;140367041064096backward -->\n",
       "<g id=\"edge13\" class=\"edge\"><title>140367041064144backward&#45;&gt;140367041064096backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M187.288,-449.165C192.464,-435.898 198.685,-419.952 204.248,-405.693\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"207.599,-406.733 207.973,-396.145 201.078,-404.189 207.599,-406.733\"/>\n",
       "<text text-anchor=\"middle\" x=\"217.96\" y=\"-419.675\" font-family=\"Menlo\" font-size=\"14.00\">4.00</text>\n",
       "</g>\n",
       "<!-- 140367041063712backward&#45;&gt;140367041063568backward -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>140367041063712backward&#45;&gt;140367041063568backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M94.1025,-112.509C94.1025,-92.0979 94.1025,-65.346 94.1025,-48.1524\"/>\n",
       "</g>\n",
       "<!-- 140367041063760backward -->\n",
       "<g id=\"node10\" class=\"node\"><title>140367041063760backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"157,-0.5 157,-59.4219 233.205,-59.4219 233.205,-0.5 157,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"195.103\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;9.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"157,-39.7812 233.205,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"195.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"157,-20.1406 233.205,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"195.103\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">a</text>\n",
       "</g>\n",
       "<!-- 140367041063712backward&#45;&gt;140367041063760backward -->\n",
       "<g id=\"edge10\" class=\"edge\"><title>140367041063712backward&#45;&gt;140367041063760backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M120.375,-112.509C133.117,-98.6039 148.556,-81.7561 162.085,-66.9923\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"164.786,-69.2257 168.961,-59.4883 159.625,-64.4964 164.786,-69.2257\"/>\n",
       "<text text-anchor=\"middle\" x=\"172.174\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.50</text>\n",
       "</g>\n",
       "<!-- 140367041063808backward -->\n",
       "<g id=\"node12\" class=\"node\"><title>140367041063808backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M270,-112.719C270,-112.719 322.205,-112.719 322.205,-112.719 328.205,-112.719 334.205,-118.719 334.205,-124.719 334.205,-124.719 334.205,-159.641 334.205,-159.641 334.205,-165.641 328.205,-171.641 322.205,-171.641 322.205,-171.641 270,-171.641 270,-171.641 264,-171.641 258,-165.641 258,-159.641 258,-159.641 258,-124.719 258,-124.719 258,-118.719 264,-112.719 270,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"258,-152 334.205,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"258,-132.359 334.205,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140367041064288backward&#45;&gt;140367041063808backward -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>140367041064288backward&#45;&gt;140367041063808backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M304.241,-224.727C302.93,-211.588 301.356,-195.821 299.944,-181.667\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"303.425,-181.31 298.95,-171.707 296.46,-182.005 303.425,-181.31\"/>\n",
       "<text text-anchor=\"middle\" x=\"323.174\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.00</text>\n",
       "</g>\n",
       "<!-- 140367041064288backward&#45;&gt;140367041063904backward -->\n",
       "<g id=\"edge7\" class=\"edge\"><title>140367041064288backward&#45;&gt;140367041063904backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M273.68,-224.914C267.018,-218.946 260.166,-212.593 253.959,-206.438 245.444,-197.994 236.609,-188.541 228.515,-179.57\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"230.891,-176.975 221.619,-171.847 225.669,-181.637 230.891,-176.975\"/>\n",
       "<text text-anchor=\"middle\" x=\"275.174\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.00</text>\n",
       "</g>\n",
       "<!-- 140367041063808backward&#45;&gt;140367041063616backward -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>140367041063808backward&#45;&gt;140367041063616backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M296.103,-112.509C296.103,-92.0979 296.103,-65.346 296.103,-48.1524\"/>\n",
       "</g>\n",
       "<!-- 140367041063808backward&#45;&gt;140367041063760backward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>140367041063808backward&#45;&gt;140367041063760backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M267.508,-112.645C261.634,-106.626 255.542,-100.265 249.959,-94.2188 242.027,-85.6275 233.664,-76.1899 225.924,-67.2859\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"228.496,-64.9096 219.309,-59.6333 223.2,-69.4875 228.496,-64.9096\"/>\n",
       "<text text-anchor=\"middle\" x=\"271.174\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">&#45;8.00</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fa9bf9eee80>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义训练数据\n",
    "x1 = Scalar(1.5, label='x1', requires_grad=False)\n",
    "y1 = Scalar(1.0, label='y1', requires_grad=False)\n",
    "x2 = Scalar(2.0, label='x2', requires_grad=False)\n",
    "y2 = Scalar(4.0, label='y2', requires_grad=False)\n",
    "# 定义正常的计算图\n",
    "model = Linear()\n",
    "k = model.forward(x1)\n",
    "l = y1 - k\n",
    "loss = mse([l, model.error(x2, y2)])\n",
    "# 反向传播\n",
    "loss.backward()\n",
    "draw_graph(loss, 'backward')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"376pt\" height=\"629pt\"\n",
       " viewBox=\"0.00 0.00 375.79 629.02\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 625.016)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-625.016 371.792,-625.016 371.792,4 -4,4\"/>\n",
       "<!-- 140367041130064backward -->\n",
       "<g id=\"node1\" class=\"node\"><title>140367041130064backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"2,-112.719 2,-171.641 78.2051,-171.641 78.2051,-112.719 2,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"40.1025\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"2,-152 78.2051,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"40.1025\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"2,-132.359 78.2051,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"40.1025\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140367041130112backward -->\n",
       "<g id=\"node2\" class=\"node\"><title>140367041130112backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"102,-0.5 102,-59.4219 178.205,-59.4219 178.205,-0.5 102,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"140.103\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;8.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"102,-39.7812 178.205,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"140.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"102,-20.1406 178.205,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"140.103\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">a</text>\n",
       "</g>\n",
       "<!-- 140367041130064backward&#45;&gt;140367041130112backward -->\n",
       "<g id=\"edge8\" class=\"edge\"><title>140367041130064backward&#45;&gt;140367041130112backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M66.1153,-112.509C78.731,-98.6039 94.0168,-81.7561 107.412,-66.9923\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"110.093,-69.2462 114.22,-59.4883 104.908,-64.5426 110.093,-69.2462\"/>\n",
       "<text text-anchor=\"middle\" x=\"112.96\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">0.00</text>\n",
       "</g>\n",
       "<!-- 140367041129920backward -->\n",
       "<g id=\"node13\" class=\"node\"><title>140367041129920backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"40.1025\" cy=\"-29.9609\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"40.1025\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">x1=1.50</text>\n",
       "</g>\n",
       "<!-- 140367041130064backward&#45;&gt;140367041129920backward -->\n",
       "<g id=\"edge15\" class=\"edge\"><title>140367041130064backward&#45;&gt;140367041129920backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M40.1025,-112.509C40.1025,-92.0979 40.1025,-65.346 40.1025,-48.1524\"/>\n",
       "</g>\n",
       "<!-- 140367041061312backward -->\n",
       "<g id=\"node3\" class=\"node\"><title>140367041061312backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M216,-224.938C216,-224.938 268.205,-224.938 268.205,-224.938 274.205,-224.938 280.205,-230.938 280.205,-236.938 280.205,-236.938 280.205,-271.859 280.205,-271.859 280.205,-277.859 274.205,-283.859 268.205,-283.859 268.205,-283.859 216,-283.859 216,-283.859 210,-283.859 204,-277.859 204,-271.859 204,-271.859 204,-236.938 204,-236.938 204,-230.938 210,-224.938 216,-224.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"242.103\" y=\"-271.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"204,-264.219 280.205,-264.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"242.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"204,-244.578 280.205,-244.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"242.103\" y=\"-232.578\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140367041130256backward -->\n",
       "<g id=\"node6\" class=\"node\"><title>140367041130256backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M214,-112.719C214,-112.719 266.205,-112.719 266.205,-112.719 272.205,-112.719 278.205,-118.719 278.205,-124.719 278.205,-124.719 278.205,-159.641 278.205,-159.641 278.205,-165.641 272.205,-171.641 266.205,-171.641 266.205,-171.641 214,-171.641 214,-171.641 208,-171.641 202,-165.641 202,-159.641 202,-159.641 202,-124.719 202,-124.719 202,-118.719 208,-112.719 214,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"240.103\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"202,-152 278.205,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"240.103\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"202,-132.359 278.205,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"240.103\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140367041061312backward&#45;&gt;140367041130256backward -->\n",
       "<g id=\"edge9\" class=\"edge\"><title>140367041061312backward&#45;&gt;140367041130256backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M241.582,-224.727C241.346,-211.716 241.063,-196.127 240.808,-182.08\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"244.301,-181.642 240.62,-171.707 237.302,-181.769 244.301,-181.642\"/>\n",
       "<text text-anchor=\"middle\" x=\"262.174\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.00</text>\n",
       "</g>\n",
       "<!-- 140367041130352backward -->\n",
       "<g id=\"node8\" class=\"node\"><title>140367041130352backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"104,-112.719 104,-171.641 180.205,-171.641 180.205,-112.719 104,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"142.103\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"104,-152 180.205,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"142.103\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"104,-132.359 180.205,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"142.103\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">b</text>\n",
       "</g>\n",
       "<!-- 140367041061312backward&#45;&gt;140367041130352backward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>140367041061312backward&#45;&gt;140367041130352backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M211.794,-224.842C205.761,-218.88 199.563,-212.547 193.959,-206.438 186.242,-198.024 178.258,-188.657 170.936,-179.765\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"173.454,-177.314 164.424,-171.773 168.027,-181.736 173.454,-177.314\"/>\n",
       "<text text-anchor=\"middle\" x=\"215.174\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.00</text>\n",
       "</g>\n",
       "<!-- 140367041130208backward -->\n",
       "<g id=\"node4\" class=\"node\"><title>140367041130208backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M173,-449.375C173,-449.375 225.205,-449.375 225.205,-449.375 231.205,-449.375 237.205,-455.375 237.205,-461.375 237.205,-461.375 237.205,-496.297 237.205,-496.297 237.205,-502.297 231.205,-508.297 225.205,-508.297 225.205,-508.297 173,-508.297 173,-508.297 167,-508.297 161,-502.297 161,-496.297 161,-496.297 161,-461.375 161,-461.375 161,-455.375 167,-449.375 173,-449.375\"/>\n",
       "<text text-anchor=\"middle\" x=\"199.103\" y=\"-496.297\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"161,-488.656 237.205,-488.656 \"/>\n",
       "<text text-anchor=\"middle\" x=\"199.103\" y=\"-476.656\" font-family=\"Menlo\" font-size=\"10.00\">value=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"161,-469.016 237.205,-469.016 \"/>\n",
       "<text text-anchor=\"middle\" x=\"199.103\" y=\"-457.016\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140367041129824backward -->\n",
       "<g id=\"node7\" class=\"node\"><title>140367041129824backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"199.103\" cy=\"-366.617\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"199.103\" y=\"-364.438\" font-family=\"Menlo\" font-size=\"10.00\">y1=1.00</text>\n",
       "</g>\n",
       "<!-- 140367041130208backward&#45;&gt;140367041129824backward -->\n",
       "<g id=\"edge7\" class=\"edge\"><title>140367041130208backward&#45;&gt;140367041129824backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M199.103,-449.165C199.103,-428.754 199.103,-402.002 199.103,-384.809\"/>\n",
       "</g>\n",
       "<!-- 140367041129344backward -->\n",
       "<g id=\"node9\" class=\"node\"><title>140367041129344backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"70,-337.156 70,-396.078 146.205,-396.078 146.205,-337.156 70,-337.156\"/>\n",
       "<text text-anchor=\"middle\" x=\"108.103\" y=\"-384.078\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"70,-376.438 146.205,-376.438 \"/>\n",
       "<text text-anchor=\"middle\" x=\"108.103\" y=\"-364.438\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"70,-356.797 146.205,-356.797 \"/>\n",
       "<text text-anchor=\"middle\" x=\"108.103\" y=\"-344.797\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140367041130208backward&#45;&gt;140367041129344backward -->\n",
       "<g id=\"edge12\" class=\"edge\"><title>140367041130208backward&#45;&gt;140367041129344backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M169.554,-449.207C163.87,-443.303 158.093,-437.011 152.959,-430.875 146.085,-422.659 139.15,-413.439 132.857,-404.633\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"135.657,-402.531 127.043,-396.363 129.931,-406.557 135.657,-402.531\"/>\n",
       "<text text-anchor=\"middle\" x=\"174.174\" y=\"-419.675\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140367041063664backward -->\n",
       "<g id=\"node5\" class=\"node\"><title>140367041063664backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M264,-337.156C264,-337.156 316.205,-337.156 316.205,-337.156 322.205,-337.156 328.205,-343.156 328.205,-349.156 328.205,-349.156 328.205,-384.078 328.205,-384.078 328.205,-390.078 322.205,-396.078 316.205,-396.078 316.205,-396.078 264,-396.078 264,-396.078 258,-396.078 252,-390.078 252,-384.078 252,-384.078 252,-349.156 252,-349.156 252,-343.156 258,-337.156 264,-337.156\"/>\n",
       "<text text-anchor=\"middle\" x=\"290.103\" y=\"-384.078\" font-family=\"Menlo\" font-size=\"10.00\">grad=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"252,-376.438 328.205,-376.438 \"/>\n",
       "<text text-anchor=\"middle\" x=\"290.103\" y=\"-364.438\" font-family=\"Menlo\" font-size=\"10.00\">value=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"252,-356.797 328.205,-356.797 \"/>\n",
       "<text text-anchor=\"middle\" x=\"290.103\" y=\"-344.797\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140367041063664backward&#45;&gt;140367041061312backward -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>140367041063664backward&#45;&gt;140367041061312backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M271.583,-337.134C268.119,-331.166 264.723,-324.813 261.959,-318.656 258.401,-310.732 255.215,-301.982 252.491,-293.582\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"255.811,-292.471 249.511,-283.953 249.124,-294.54 255.811,-292.471\"/>\n",
       "<text text-anchor=\"middle\" x=\"282.174\" y=\"-307.456\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.00</text>\n",
       "</g>\n",
       "<!-- 140367041129872backward -->\n",
       "<g id=\"node10\" class=\"node\"><title>140367041129872backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"333.103\" cy=\"-254.398\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"333.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">y2=4.00</text>\n",
       "</g>\n",
       "<!-- 140367041063664backward&#45;&gt;140367041129872backward -->\n",
       "<g id=\"edge11\" class=\"edge\"><title>140367041063664backward&#45;&gt;140367041129872backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M301.288,-336.946C309.301,-316.408 319.818,-289.449 326.521,-272.269\"/>\n",
       "</g>\n",
       "<!-- 140367041130256backward&#45;&gt;140367041130112backward -->\n",
       "<g id=\"edge13\" class=\"edge\"><title>140367041130256backward&#45;&gt;140367041130112backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M211.465,-112.684C205.594,-106.662 199.514,-100.291 193.959,-94.2188 186.128,-85.6587 177.901,-76.2326 170.3,-67.3299\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"172.945,-65.0377 163.807,-59.676 167.607,-69.5659 172.945,-65.0377\"/>\n",
       "<text text-anchor=\"middle\" x=\"215.174\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">&#45;8.00</text>\n",
       "</g>\n",
       "<!-- 140367041129968backward -->\n",
       "<g id=\"node15\" class=\"node\"><title>140367041129968backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"240.103\" cy=\"-29.9609\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"240.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">x2=2.00</text>\n",
       "</g>\n",
       "<!-- 140367041130256backward&#45;&gt;140367041129968backward -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>140367041130256backward&#45;&gt;140367041129968backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M240.103,-112.509C240.103,-92.0979 240.103,-65.346 240.103,-48.1524\"/>\n",
       "</g>\n",
       "<!-- 140367041130400backward -->\n",
       "<g id=\"node11\" class=\"node\"><title>140367041130400backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"0,-224.938 0,-283.859 76.2051,-283.859 76.2051,-224.938 0,-224.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-271.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"0,-264.219 76.2051,-264.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"0,-244.578 76.2051,-244.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-232.578\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140367041129344backward&#45;&gt;140367041130400backward -->\n",
       "<g id=\"edge10\" class=\"edge\"><title>140367041129344backward&#45;&gt;140367041130400backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M89.8936,-336.946C81.3057,-323.424 70.9501,-307.119 61.7632,-292.653\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"64.536,-290.491 56.2203,-283.926 58.627,-294.244 64.536,-290.491\"/>\n",
       "<text text-anchor=\"middle\" x=\"94.96\" y=\"-307.456\" font-family=\"Menlo\" font-size=\"14.00\">0.00</text>\n",
       "</g>\n",
       "<!-- 140367041130448backward -->\n",
       "<g id=\"node14\" class=\"node\"><title>140367041130448backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"140.103\" cy=\"-254.398\" rx=\"45.43\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"140.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">input=0.00</text>\n",
       "</g>\n",
       "<!-- 140367041129344backward&#45;&gt;140367041130448backward -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>140367041129344backward&#45;&gt;140367041130448backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M116.427,-336.946C122.353,-316.535 130.119,-289.784 135.111,-272.59\"/>\n",
       "</g>\n",
       "<!-- 140367041130400backward&#45;&gt;140367041130064backward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>140367041130400backward&#45;&gt;140367041130064backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M38.6228,-224.727C38.8589,-211.716 39.1418,-196.127 39.3967,-182.08\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"42.9028,-181.769 39.5849,-171.707 35.9039,-181.642 42.9028,-181.769\"/>\n",
       "<text text-anchor=\"middle\" x=\"56.96\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">0.00</text>\n",
       "</g>\n",
       "<!-- 140367041130400backward&#45;&gt;140367041130352backward -->\n",
       "<g id=\"edge14\" class=\"edge\"><title>140367041130400backward&#45;&gt;140367041130352backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M65.1558,-224.727C78.2762,-210.823 94.1734,-193.975 108.104,-179.211\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"110.867,-181.382 115.185,-171.707 105.776,-176.578 110.867,-181.382\"/>\n",
       "<text text-anchor=\"middle\" x=\"113.96\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">0.00</text>\n",
       "</g>\n",
       "<!-- 140367041063856backward -->\n",
       "<g id=\"node12\" class=\"node\"><title>140367041063856backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M222,-561.594C222,-561.594 274.205,-561.594 274.205,-561.594 280.205,-561.594 286.205,-567.594 286.205,-573.594 286.205,-573.594 286.205,-608.516 286.205,-608.516 286.205,-614.516 280.205,-620.516 274.205,-620.516 274.205,-620.516 222,-620.516 222,-620.516 216,-620.516 210,-614.516 210,-608.516 210,-608.516 210,-573.594 210,-573.594 210,-567.594 216,-561.594 222,-561.594\"/>\n",
       "<text text-anchor=\"middle\" x=\"248.103\" y=\"-608.516\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"210,-600.875 286.205,-600.875 \"/>\n",
       "<text text-anchor=\"middle\" x=\"248.103\" y=\"-588.875\" font-family=\"Menlo\" font-size=\"10.00\">value=8.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"210,-581.234 286.205,-581.234 \"/>\n",
       "<text text-anchor=\"middle\" x=\"248.103\" y=\"-569.234\" font-family=\"Menlo\" font-size=\"10.00\">mse</text>\n",
       "</g>\n",
       "<!-- 140367041063856backward&#45;&gt;140367041130208backward -->\n",
       "<g id=\"edge16\" class=\"edge\"><title>140367041063856backward&#45;&gt;140367041130208backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M235.356,-561.384C229.458,-548.117 222.369,-532.171 216.03,-517.912\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"219.046,-516.079 211.785,-508.363 212.649,-518.923 219.046,-516.079\"/>\n",
       "<text text-anchor=\"middle\" x=\"242.96\" y=\"-531.894\" font-family=\"Menlo\" font-size=\"14.00\">1.00</text>\n",
       "</g>\n",
       "<!-- 140367041063856backward&#45;&gt;140367041063664backward -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>140367041063856backward&#45;&gt;140367041063664backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M258.754,-561.489C260.737,-555.472 262.642,-549.121 264.103,-543.094 275.358,-496.646 282.521,-442.03 286.418,-406.216\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"289.908,-406.498 287.477,-396.186 282.946,-405.763 289.908,-406.498\"/>\n",
       "<text text-anchor=\"middle\" x=\"297.96\" y=\"-475.784\" font-family=\"Menlo\" font-size=\"14.00\">4.00</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fa9bf9ee100>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1 = Scalar(1.5, label='x1', requires_grad=False)\n",
    "y1 = Scalar(1.0, label='y1', requires_grad=False)\n",
    "x2 = Scalar(2.0, label='x2', requires_grad=False)\n",
    "y2 = Scalar(4.0, label='y2', requires_grad=False)\n",
    "model = Linear()\n",
    "k = model.forward(x1)\n",
    "# 将k失活\n",
    "k_out = k * 0\n",
    "l = y1 - k_out\n",
    "loss = mse([l, model.error(x2, y2)])\n",
    "# 反向传播\n",
    "loss.backward()\n",
    "draw_graph(loss, 'backward')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"405pt\" height=\"590pt\"\n",
       " viewBox=\"0.00 0.00 404.57 589.80\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 585.797)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-585.797 400.568,-585.797 400.568,4 -4,4\"/>\n",
       "<!-- 140367041130016backward -->\n",
       "<g id=\"node1\" class=\"node\"><title>140367041130016backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M99,-410.156C99,-410.156 151.205,-410.156 151.205,-410.156 157.205,-410.156 163.205,-416.156 163.205,-422.156 163.205,-422.156 163.205,-457.078 163.205,-457.078 163.205,-463.078 157.205,-469.078 151.205,-469.078 151.205,-469.078 99,-469.078 99,-469.078 93,-469.078 87,-463.078 87,-457.078 87,-457.078 87,-422.156 87,-422.156 87,-416.156 93,-410.156 99,-410.156\"/>\n",
       "<text text-anchor=\"middle\" x=\"125.103\" y=\"-457.078\" font-family=\"Menlo\" font-size=\"10.00\">grad=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"87,-449.438 163.205,-449.438 \"/>\n",
       "<text text-anchor=\"middle\" x=\"125.103\" y=\"-437.438\" font-family=\"Menlo\" font-size=\"10.00\">value=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"87,-429.797 163.205,-429.797 \"/>\n",
       "<text text-anchor=\"middle\" x=\"125.103\" y=\"-417.797\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140367038669008backward -->\n",
       "<g id=\"node7\" class=\"node\"><title>140367038669008backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M12,-297.938C12,-297.938 64.2051,-297.938 64.2051,-297.938 70.2051,-297.938 76.2051,-303.938 76.2051,-309.938 76.2051,-309.938 76.2051,-344.859 76.2051,-344.859 76.2051,-350.859 70.2051,-356.859 64.2051,-356.859 64.2051,-356.859 12,-356.859 12,-356.859 6,-356.859 0,-350.859 0,-344.859 0,-344.859 0,-309.938 0,-309.938 0,-303.938 6,-297.938 12,-297.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-344.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"0,-337.219 76.2051,-337.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-325.219\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"0,-317.578 76.2051,-317.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-305.578\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140367041130016backward&#45;&gt;140367038669008backward -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>140367041130016backward&#45;&gt;140367038669008backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M96.8068,-409.954C91.3717,-404.053 85.8534,-397.771 80.959,-391.656 74.4313,-383.5 67.8642,-374.376 61.8997,-365.652\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"64.6426,-363.458 56.1598,-357.11 58.8327,-367.363 64.6426,-363.458\"/>\n",
       "<text text-anchor=\"middle\" x=\"102.174\" y=\"-380.456\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.00</text>\n",
       "</g>\n",
       "<!-- 140367038537632backward -->\n",
       "<g id=\"node12\" class=\"node\"><title>140367038537632backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"129.103\" cy=\"-327.398\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"129.103\" y=\"-325.219\" font-family=\"Menlo\" font-size=\"10.00\">y2=4.00</text>\n",
       "</g>\n",
       "<!-- 140367041130016backward&#45;&gt;140367038537632backward -->\n",
       "<g id=\"edge9\" class=\"edge\"><title>140367041130016backward&#45;&gt;140367038537632backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M126.143,-409.946C126.884,-389.535 127.855,-362.784 128.479,-345.59\"/>\n",
       "</g>\n",
       "<!-- 140367038669872backward -->\n",
       "<g id=\"node2\" class=\"node\"><title>140367038669872backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"258,-73.5 258,-132.422 334.205,-132.422 334.205,-73.5 258,-73.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-120.422\" font-family=\"Menlo\" font-size=\"10.00\">grad=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"258,-112.781 334.205,-112.781 \"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-100.781\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"258,-93.1406 334.205,-93.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-81.1406\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140367038671456backward -->\n",
       "<g id=\"node4\" class=\"node\"><title>140367038671456backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"242.103\" cy=\"-18\" rx=\"45.43\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"242.103\" y=\"-15.8203\" font-family=\"Menlo\" font-size=\"10.00\">input=0.00</text>\n",
       "</g>\n",
       "<!-- 140367038669872backward&#45;&gt;140367038671456backward -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>140367038669872backward&#45;&gt;140367038671456backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M277.519,-73.4105C269.343,-60.8492 260.022,-46.5303 252.996,-35.7353\"/>\n",
       "</g>\n",
       "<!-- 140367038536480backward -->\n",
       "<g id=\"node9\" class=\"node\"><title>140367038536480backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"351.103\" cy=\"-18\" rx=\"45.43\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"351.103\" y=\"-15.8203\" font-family=\"Menlo\" font-size=\"10.00\">input=1.50</text>\n",
       "</g>\n",
       "<!-- 140367038669872backward&#45;&gt;140367038536480backward -->\n",
       "<g id=\"edge16\" class=\"edge\"><title>140367038669872backward&#45;&gt;140367038536480backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M315.03,-73.4105C323.358,-60.8492 332.851,-46.5303 340.008,-35.7353\"/>\n",
       "</g>\n",
       "<!-- 140367038668864backward -->\n",
       "<g id=\"node3\" class=\"node\"><title>140367038668864backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M68,-185.719C68,-185.719 120.205,-185.719 120.205,-185.719 126.205,-185.719 132.205,-191.719 132.205,-197.719 132.205,-197.719 132.205,-232.641 132.205,-232.641 132.205,-238.641 126.205,-244.641 120.205,-244.641 120.205,-244.641 68,-244.641 68,-244.641 62,-244.641 56,-238.641 56,-232.641 56,-232.641 56,-197.719 56,-197.719 56,-191.719 62,-185.719 68,-185.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"94.1025\" y=\"-232.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"56,-225 132.205,-225 \"/>\n",
       "<text text-anchor=\"middle\" x=\"94.1025\" y=\"-213\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"56,-205.359 132.205,-205.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"94.1025\" y=\"-193.359\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140367038669104backward -->\n",
       "<g id=\"node10\" class=\"node\"><title>140367038669104backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"156,-73.5 156,-132.422 232.205,-132.422 232.205,-73.5 156,-73.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"194.103\" y=\"-120.422\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;8.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"156,-112.781 232.205,-112.781 \"/>\n",
       "<text text-anchor=\"middle\" x=\"194.103\" y=\"-100.781\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"156,-93.1406 232.205,-93.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"194.103\" y=\"-81.1406\" font-family=\"Menlo\" font-size=\"10.00\">a</text>\n",
       "</g>\n",
       "<!-- 140367038668864backward&#45;&gt;140367038669104backward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>140367038668864backward&#45;&gt;140367038669104backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M120.115,-185.509C132.731,-171.604 148.017,-154.756 161.412,-139.992\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"164.093,-142.246 168.22,-132.488 158.908,-137.543 164.093,-142.246\"/>\n",
       "<text text-anchor=\"middle\" x=\"171.174\" y=\"-156.019\" font-family=\"Menlo\" font-size=\"14.00\">&#45;8.00</text>\n",
       "</g>\n",
       "<!-- 140367038669776backward -->\n",
       "<g id=\"node14\" class=\"node\"><title>140367038669776backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"94.1025\" cy=\"-102.961\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"94.1025\" y=\"-100.781\" font-family=\"Menlo\" font-size=\"10.00\">x2=2.00</text>\n",
       "</g>\n",
       "<!-- 140367038668864backward&#45;&gt;140367038669776backward -->\n",
       "<g id=\"edge15\" class=\"edge\"><title>140367038668864backward&#45;&gt;140367038669776backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M94.1025,-185.509C94.1025,-165.098 94.1025,-138.346 94.1025,-121.152\"/>\n",
       "</g>\n",
       "<!-- 140367038669440backward -->\n",
       "<g id=\"node5\" class=\"node\"><title>140367038669440backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"159,-185.719 159,-244.641 235.205,-244.641 235.205,-185.719 159,-185.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"197.103\" y=\"-232.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;5.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"159,-225 235.205,-225 \"/>\n",
       "<text text-anchor=\"middle\" x=\"197.103\" y=\"-213\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"159,-205.359 235.205,-205.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"197.103\" y=\"-193.359\" font-family=\"Menlo\" font-size=\"10.00\">b</text>\n",
       "</g>\n",
       "<!-- 140367038670016backward -->\n",
       "<g id=\"node6\" class=\"node\"><title>140367038670016backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M193,-410.156C193,-410.156 245.205,-410.156 245.205,-410.156 251.205,-410.156 257.205,-416.156 257.205,-422.156 257.205,-422.156 257.205,-457.078 257.205,-457.078 257.205,-463.078 251.205,-469.078 245.205,-469.078 245.205,-469.078 193,-469.078 193,-469.078 187,-469.078 181,-463.078 181,-457.078 181,-457.078 181,-422.156 181,-422.156 181,-416.156 187,-410.156 193,-410.156\"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-457.078\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"181,-449.438 257.205,-449.438 \"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-437.438\" font-family=\"Menlo\" font-size=\"10.00\">value=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"181,-429.797 257.205,-429.797 \"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-417.797\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140367038535952backward -->\n",
       "<g id=\"node8\" class=\"node\"><title>140367038535952backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"216.103\" cy=\"-327.398\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"216.103\" y=\"-325.219\" font-family=\"Menlo\" font-size=\"10.00\">y1=1.00</text>\n",
       "</g>\n",
       "<!-- 140367038670016backward&#45;&gt;140367038535952backward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>140367038670016backward&#45;&gt;140367038535952backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M218.322,-409.946C217.767,-389.535 217.038,-362.784 216.57,-345.59\"/>\n",
       "</g>\n",
       "<!-- 140367038669296backward -->\n",
       "<g id=\"node15\" class=\"node\"><title>140367038669296backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M281,-297.938C281,-297.938 333.205,-297.938 333.205,-297.938 339.205,-297.938 345.205,-303.938 345.205,-309.938 345.205,-309.938 345.205,-344.859 345.205,-344.859 345.205,-350.859 339.205,-356.859 333.205,-356.859 333.205,-356.859 281,-356.859 281,-356.859 275,-356.859 269,-350.859 269,-344.859 269,-344.859 269,-309.938 269,-309.938 269,-303.938 275,-297.938 281,-297.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"307.103\" y=\"-344.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"269,-337.219 345.205,-337.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"307.103\" y=\"-325.219\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"269,-317.578 345.205,-317.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"307.103\" y=\"-305.578\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140367038670016backward&#45;&gt;140367038669296backward -->\n",
       "<g id=\"edge14\" class=\"edge\"><title>140367038670016backward&#45;&gt;140367038669296backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M241.994,-409.946C252.994,-396.169 266.3,-379.502 278.01,-364.837\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"280.822,-366.924 284.326,-356.926 275.351,-362.557 280.822,-366.924\"/>\n",
       "<text text-anchor=\"middle\" x=\"290.174\" y=\"-380.456\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140367038669008backward&#45;&gt;140367038668864backward -->\n",
       "<g id=\"edge10\" class=\"edge\"><title>140367038669008backward&#45;&gt;140367038668864backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M52.6697,-297.727C59.4752,-284.333 67.6683,-268.207 74.9657,-253.845\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"78.1989,-255.208 79.6083,-244.707 71.9582,-252.037 78.1989,-255.208\"/>\n",
       "<text text-anchor=\"middle\" x=\"91.1743\" y=\"-268.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.00</text>\n",
       "</g>\n",
       "<!-- 140367038669008backward&#45;&gt;140367038669440backward -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>140367038669008backward&#45;&gt;140367038669440backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M76.2711,-302.764C79.2538,-300.952 82.2239,-299.16 85.1025,-297.438 98.7744,-289.258 102.904,-288.361 116.103,-279.438 129.076,-270.665 142.676,-260.472 154.946,-250.863\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"157.125,-253.603 162.798,-244.654 152.783,-248.112 157.125,-253.603\"/>\n",
       "<text text-anchor=\"middle\" x=\"160.174\" y=\"-268.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.00</text>\n",
       "</g>\n",
       "<!-- 140367041130304backward -->\n",
       "<g id=\"node11\" class=\"node\"><title>140367041130304backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M150,-522.375C150,-522.375 202.205,-522.375 202.205,-522.375 208.205,-522.375 214.205,-528.375 214.205,-534.375 214.205,-534.375 214.205,-569.297 214.205,-569.297 214.205,-575.297 208.205,-581.297 202.205,-581.297 202.205,-581.297 150,-581.297 150,-581.297 144,-581.297 138,-575.297 138,-569.297 138,-569.297 138,-534.375 138,-534.375 138,-528.375 144,-522.375 150,-522.375\"/>\n",
       "<text text-anchor=\"middle\" x=\"176.103\" y=\"-569.297\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"138,-561.656 214.205,-561.656 \"/>\n",
       "<text text-anchor=\"middle\" x=\"176.103\" y=\"-549.656\" font-family=\"Menlo\" font-size=\"10.00\">value=8.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"138,-542.016 214.205,-542.016 \"/>\n",
       "<text text-anchor=\"middle\" x=\"176.103\" y=\"-530.016\" font-family=\"Menlo\" font-size=\"10.00\">mse</text>\n",
       "</g>\n",
       "<!-- 140367041130304backward&#45;&gt;140367041130016backward -->\n",
       "<g id=\"edge13\" class=\"edge\"><title>140367041130304backward&#45;&gt;140367041130016backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M162.836,-522.165C156.638,-508.77 149.177,-492.645 142.531,-478.282\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"145.679,-476.75 138.303,-469.145 139.326,-479.69 145.679,-476.75\"/>\n",
       "<text text-anchor=\"middle\" x=\"170.96\" y=\"-492.675\" font-family=\"Menlo\" font-size=\"14.00\">4.00</text>\n",
       "</g>\n",
       "<!-- 140367041130304backward&#45;&gt;140367038670016backward -->\n",
       "<g id=\"edge7\" class=\"edge\"><title>140367041130304backward&#45;&gt;140367038670016backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M187.288,-522.165C192.464,-508.898 198.685,-492.952 204.248,-478.693\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"207.599,-479.733 207.973,-469.145 201.078,-477.189 207.599,-479.733\"/>\n",
       "<text text-anchor=\"middle\" x=\"217.96\" y=\"-492.675\" font-family=\"Menlo\" font-size=\"14.00\">1.00</text>\n",
       "</g>\n",
       "<!-- 140367038671792backward -->\n",
       "<g id=\"node13\" class=\"node\"><title>140367038671792backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"258,-185.719 258,-244.641 334.205,-244.641 334.205,-185.719 258,-185.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-232.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"258,-225 334.205,-225 \"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-213\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"258,-205.359 334.205,-205.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"296.103\" y=\"-193.359\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140367038671792backward&#45;&gt;140367038669872backward -->\n",
       "<g id=\"edge8\" class=\"edge\"><title>140367038671792backward&#45;&gt;140367038669872backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M296.103,-185.509C296.103,-169.18 296.103,-148.793 296.103,-132.488\"/>\n",
       "</g>\n",
       "<!-- 140367038671792backward&#45;&gt;140367038669104backward -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>140367038671792backward&#45;&gt;140367038669104backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M269.57,-185.509C256.701,-171.604 241.11,-154.756 227.447,-139.992\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"229.864,-137.451 220.503,-132.488 224.726,-142.205 229.864,-137.451\"/>\n",
       "<text text-anchor=\"middle\" x=\"268.96\" y=\"-156.019\" font-family=\"Menlo\" font-size=\"14.00\">0.00</text>\n",
       "</g>\n",
       "<!-- 140367038669296backward&#45;&gt;140367038669440backward -->\n",
       "<g id=\"edge11\" class=\"edge\"><title>140367038669296backward&#45;&gt;140367038669440backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M273.68,-297.914C267.018,-291.946 260.166,-285.593 253.959,-279.438 245.444,-270.994 236.609,-261.541 228.515,-252.57\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"230.891,-249.975 221.619,-244.847 225.669,-254.637 230.891,-249.975\"/>\n",
       "<text text-anchor=\"middle\" x=\"275.174\" y=\"-268.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140367038669296backward&#45;&gt;140367038671792backward -->\n",
       "<g id=\"edge12\" class=\"edge\"><title>140367038669296backward&#45;&gt;140367038671792backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M304.241,-297.727C302.93,-284.588 301.356,-268.821 299.944,-254.667\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"303.425,-254.31 298.95,-244.707 296.46,-255.005 303.425,-254.31\"/>\n",
       "<text text-anchor=\"middle\" x=\"323.174\" y=\"-268.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fa9bf9fe190>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 为了减少计算图的歧义，将x1的标签省略掉\n",
    "x1 = Scalar(1.5, requires_grad=False)\n",
    "y1 = Scalar(1.0, label='y1', requires_grad=False)\n",
    "x2 = Scalar(2.0, label='x2', requires_grad=False)\n",
    "y2 = Scalar(4.0, label='y2', requires_grad=False)\n",
    "# 将变量x1失活\n",
    "x1_out = x1 * 0\n",
    "model = Linear()\n",
    "loss = mse([model.error(x1_out, y1), model.error(x2, y2)])\n",
    "# 反向传播\n",
    "loss.backward()\n",
    "draw_graph(loss, 'backward')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
